home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 076-100 / 091 / util / virtmem.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  4KB  |  192 lines

  1. #include <stdio.h>
  2.  
  3. #include "adltypes.h"
  4. #include "virtmem.h"
  5.  
  6. /* Macros to find the page number and offset of an address */
  7. #define SEGMENT( where )    ((where >> 9) & 0x7FFF)
  8. #define OFFSET( where )        (where & 0x01FF)
  9.  
  10. vm_init( which, offs, handle, d )
  11. int
  12.     which;        /* Paging file */
  13. int32
  14.     offs;        /* Starting offset into the paging file */
  15. struct pagetab
  16.     *handle;        /* LRU queue */
  17. int16
  18.     d;            /* Dirty bit */
  19. {
  20.     handle->pfile = which;
  21.     handle->offset = offs;
  22.     handle->dirty = d;
  23. }
  24.  
  25.  
  26. vm_flush( handle )
  27. struct pagetab
  28.     *handle;
  29. {
  30.     int
  31.     i;
  32.  
  33.     if( handle->dirty ) {
  34.     for( i = 0; i < handle->numpages; i++ ) {
  35.         lseek(    handle->pfile,
  36.             handle->offset + (int32)(handle->ptabs[i].pnum * BSIZE),
  37.             0 );
  38.         write( handle->pfile, handle->ptabs[ i ].buffer, BSIZE );
  39.     }
  40.     }
  41. }
  42.  
  43.  
  44. char    *
  45. vm_getpg( num, handle )
  46. int
  47.     num;
  48. struct    pagetab
  49.     *handle;
  50. {
  51.     struct pt_entry
  52.     *ptr;
  53.  
  54.     /* Search for the page number in the LRU queue */
  55.     for( ptr = handle->mru; ptr; ptr = ptr->next ) {
  56.     if( ptr->pnum == num ) {
  57.         /* Page is in memory */
  58.         if( ptr != handle->mru ) {
  59.         /* Put the entry at the head of the LRU queue */
  60.         (ptr->prev)->next = ptr->next;
  61.         if( ptr->next )
  62.             (ptr->next)->prev = ptr->prev;
  63.         else
  64.             handle->lru = ptr->prev;
  65.  
  66.         ptr->next = handle->mru;
  67.         (handle->mru)->prev = ptr;
  68.  
  69.         ptr->prev = (struct pt_entry *)0;
  70.         handle->mru = ptr;
  71.         }
  72.         return ptr->buffer;
  73.     }
  74.     }
  75.  
  76.     /* If we get here, the page is not in memory.  Read it in. */
  77.     if( handle->numpages < NUMP ) {
  78.     /* We have available pages */
  79.     ptr = handle->ptabs + handle->numpages++;
  80.     if( handle->numpages == NUMP ) {
  81.         /* We just allocated the last available page.  Find the LRU. */
  82.         for(    handle->lru = handle->mru;
  83.             (handle->lru)->next;
  84.             handle->lru = (handle->lru)->next )
  85.         /* NOTHING */;
  86.     }
  87.     }
  88.     else {            /* We need to swap an old page */
  89.     /* Take the LRU off the end of the list. */
  90.     ptr = handle->lru;
  91.     handle->lru = ptr->prev;
  92.     (ptr->prev)->next = (struct pt_entry *)0;
  93.  
  94.     if( handle->dirty ) {
  95.         /* We need to write the old contents */
  96.         lseek(    handle->pfile,
  97.             handle->offset + (int32)(ptr->pnum * BSIZE),
  98.             0 );
  99.         write( handle->pfile, ptr->buffer, BSIZE );
  100.     }
  101.     }
  102.  
  103.     /* Put the old LRU at the head of the list, as the MRU */
  104.     ptr->prev = (struct pt_entry *)0;
  105.     ptr->next = handle->mru;
  106.     if( handle->mru )
  107.     (handle->mru)->prev = ptr;
  108.  
  109.     /* Change the data, then read in the page */
  110.     ptr->pnum = num;
  111.     handle->mru = ptr;
  112.     lseek( handle->pfile, handle->offset + (int32)(num * BSIZE), 0 );
  113.     read( handle->pfile, ptr->buffer, BSIZE );
  114.     return ptr->buffer;
  115. }
  116.  
  117.  
  118. vm_put8( data, where, handle )
  119. char
  120.     data;
  121. int32
  122.     where;
  123. struct pagetab
  124.     *handle;
  125. {
  126.     char
  127.     *buff;
  128.     int
  129.     seg,
  130.     offs;
  131.  
  132.     seg = SEGMENT( where );
  133.     offs = OFFSET( where );
  134.     if( seg >= handle->maxpage ) {        /* Grow the file */
  135.     lseek( handle->pfile, 0L, 2 );    /* EOF */
  136.     write( handle->pfile, handle->ptabs[0].buffer, BSIZE );
  137.     handle->maxpage++;
  138.     }
  139.     buff = vm_getpg( seg, handle );
  140.     buff[ offs ] = data;
  141. }
  142.  
  143.  
  144. vm_put16( data, where, handle )
  145. int16
  146.     data;
  147. int32
  148.     where;
  149. struct pagetab
  150.     *handle;
  151. {
  152.     vm_put8( data & 0x0ff, where, handle );
  153.     vm_put8( (data >> 8) & 0x0ff, where + 1, handle );
  154. }
  155.  
  156.  
  157. char
  158. vm_get8( where, handle )
  159. int32
  160.     where;
  161. struct pagetab
  162.     *handle;
  163. {
  164.     int
  165.     seg, offs;
  166.     char
  167.     *buff;
  168.  
  169.     seg = SEGMENT( where );
  170.     offs = OFFSET( where );
  171.     buff = vm_getpg( seg, handle );
  172.     return buff[ offs ];
  173. }
  174.  
  175.  
  176. int16
  177. vm_get16( where, handle )
  178. int32
  179.     where;
  180. struct pagetab
  181.     *handle;
  182. {
  183.     int16
  184.     t;
  185.  
  186.     t = vm_get8( where, handle ) & 0x0ff;
  187.     t |= vm_get8( where + 1, handle ) << 8;
  188.     return t;
  189. }
  190.  
  191. /*** EOF virtmem.c ***/
  192.